package com.ngocvo.QuanLyThuChi;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.EditText;
import android.widget.TextView;
/*	
 * Class name	: Global
 * Create Date	: 08-03-2011
 * Author		: Ngoc Vo
 * Description	: Includes global methods and variables
 * Modified by	: Ngoc Vo
 * Modified Date: 08-04-2011
 * Modify log	: Add comments, logs
 * 
 */

public class Global {
/*
 * ================================================================================================
 * 											VARIABLES SECTION
 * ================================================================================================
 */
	
/*
 * ******************
 * DIALOGS VARIABLES
 * ******************
 */
	static final int DATE_DIALOG_ID = 0;			//ID of flow date picker dialog
	static final int RETURN_DATE_DIALOG_ID = 1;		//ID of flow back date picker dialog
	static final int TIME_DIALOG_ID = 2;			//ID of flow time picker dialog
	
/*
 * *******************
 * DATABASE VARIABLES
 * *******************
 */	
	static final String DATABASE_NAME = "CashFlowManagement";	// database name
	static final int DATABASE_VER = 1;							// database version
	
/*
 * *******************
 * DATETIME VARIABLES
 * *******************
 */		
	static final SimpleDateFormat dbFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");	//datetime format for saving in database
	
	//Variables for system datetime
	static int intYear;											
	static int intMonth;
	static int intDay;
	static int intHour;
	static int intMinute;
	static int intSecond;
	
	static String strDate;	//date of db format yyyy-MM-dd
	static String strTime;	//time of db format HH:mm:ss
	
	static String strFlowBackDate;	//date of db format yyyy-MM-dd
	
	static String strCreateDate;	//date when a cash flow is being created
	static String strModifiedDate;	//date when a cash flow is being modified
	
/*
 * *****************
 * STRING VARIABLES
 * *****************
 */	
	static final String strKhoanThu = "Khoản thu";
	static final String strKhoanChi = "Khoản chi";
	static final String strKhoanChoVay = "Khoản cho vay";
	static final String strKhoanMuonNo = "Khoản mượn nợ";
	
	//Variables for Error Messages Dialog
	static final String strMissingInfo = "Thông tin không hợp lệ";	//dialog title
	static final String strMissingAmount = "Xin vui lòng điền số tiền hợp lệ vào.";	//dialog content
	static final String strPositiveButton = "Tiếp tục";	//text on dialog button
	
/*
 * *******
 * OTHERS
 * *******	
 */
	static FlowType flowType;	//flow type	-1|0|1|2|3	unknown|Thu|Chi|Vay|No
	static short srtStatus;		//status of cash flow (only on Vay|No): -1|0|1	Unknown|Chưa trả|Đã trả
	
	
/*
 * ================================================================================================
 * 										METHODS SECTION
 * ================================================================================================
 */
	
/*
 * ID			: 01
 * Method name	: pad
 * Create date	: 08-03-2011
 * Author		: Ngoc Vo
 * Parameter	: int c
 * Input		: 1 digit integer number. Ex: 1
 * Output		: 2 digits string. Ex: 01
 * Description	: Add a zero '0' before 1 digit number
 * Modify date	:
 * Modify log	:
 */
	public static String pad(int c) {
	    if (c >= 10)
	        return String.valueOf(c);
	    else
	        return "0" + String.valueOf(c);
	}
/*
 * ID			: 02
 * Method name	: updateDate
 * Create date	: 08-03-2011
 * Author		: Ngoc Vo
 * Parameter	: TextView tvDate
 * Input		: TextView that displays date
 * Output		: None
 * Description	: Displays updated date on input TextView
 * 				  Update strDate with update db formate date
 * Modify date	:
 * Modify log	:
 */
	public static void updateDate(TextView tvDate){
		//Update strDate with updated db format date (yyyy-MM-dd)
		strDate = String.valueOf(intYear) + "-" +
					String.valueOf(pad(intMonth)) + "-" +
					String.valueOf(pad(intDay)) + " ";
		
		//Set text of tvDate with updated date (dd-MM-yyyy)
		tvDate.setText(String.valueOf(pad(intDay)) + "-" +
						String.valueOf(pad(intMonth)) + "-" +
						String.valueOf(intYear));
				
	}
/*
 * ID			: 03
 * Method name	: updateTime
 * Create date	: 08-03-2011
 * Author		: Ngoc Vo
 * Parameter	: TextView tvTime
 * Input		: TextView that displays time
 * Output		: None
 * Description	: Displays updated time on input TextView
 * 				  Update strTime with update db formate time
 * Modify date	:
 * Modify log	:
 */	
	public static void updateTime(TextView tvTime){
		//Update strTime with updated db format time (HH:mm:ss)
		strTime = String.valueOf(pad(intHour)) + ":" +
					String.valueOf(pad(intMinute)) + ":" +
					String.valueOf(pad(intSecond));
		
		//Set text of tvTime with updated time (HH:mm)
		tvTime.setText(String.valueOf(pad(intHour)) + ":" +
						String.valueOf(pad(intMinute)));
	}
/*
 * ID			: 04
 * Method name	: updateFlowBackDate
 * Create date	: 08-03-2011
 * Author		: Ngoc Vo
 * Parameter	: TextView tvFlowBackDate
 * Input		: TextView that displays flow back date
 * Output		: None
 * Description	: Displays updated time on input TextView
 * 				  Update strTime with update db formate time
 * Modify date	:
 * Modify log	:
 */	
	public static void updateFlowBackDate(TextView tvFlowBackDate){
		//Update strFlowBackDate with updated db format date (yyyy-MM-dd)
		strFlowBackDate = String.valueOf(intYear) + "-" +
							String.valueOf(pad(intMonth)) + "-" +
							String.valueOf(pad(intDay)) + " ";
		
		//Update tvFlowBackDate with updated date (dd-MM-yyyy)
		tvFlowBackDate.setText(String.valueOf(pad(intDay)) + "-" +
								String.valueOf(pad(intMonth)) + "-" +
								String.valueOf(intYear));
	}
/*
 * ID			: 05
 * Method name	: updateCurrentDate
 * Create date	: 08-03-2011
 * Author		: Ngoc Vo
 * Parameter	: None
 * Input		: None
 * Output		: None
 * Description	: Update system datetime variables with updated values
 * 				  Update strTime with updated db format value
 * Modify date	:
 * Modify log	:
 */	
	public static void updateCurrentDate(){
		final Calendar c = Calendar.getInstance();
		intYear = c.get(Calendar.YEAR);
		intMonth = c.get(Calendar.MONTH) + 1;
		intDay = c.get(Calendar.DAY_OF_MONTH);
		intHour = c.get(Calendar.HOUR_OF_DAY);
		intMinute = c.get(Calendar.MINUTE);
		intSecond = c.get(Calendar.SECOND);
		
		strTime = String.valueOf(pad(intHour)) + ":" +
					String.valueOf(pad(intMinute)) + ":" +
					String.valueOf(pad(intSecond));
		
	}
/*
 * ID			: 06
 * Method name	: updateCurrentDate
 * Create date	: 08-03-2011
 * Author		: Ngoc Vo
 * Parameter	: TextView tvDate, TextView tvTime
 * Input		: TextView that displays date and time
 * Output		: None
 * Description	: Displays updated time on input TextView
 * 				  Update system datetime variables with updated values
 * 				  Update strDate, sttrTime with update db format values
 * Modify date	:
 * Modify log	:
 */	
	public static void updateCurrentDate(TextView tvDate, TextView tvTime){
		//Update
		final Calendar c = Calendar.getInstance();
		intYear = c.get(Calendar.YEAR);
		intMonth = c.get(Calendar.MONTH) + 1;
		intDay = c.get(Calendar.DAY_OF_MONTH);
		intHour = c.get(Calendar.HOUR_OF_DAY);
		intMinute = c.get(Calendar.MINUTE);
		intSecond = c.get(Calendar.SECOND);
		
		updateTime(tvTime);
		updateDate(tvDate);
	}
/*
 * ID			: 07
 * Method name	: updateCurrentDate
 * Create date	: 08-03-2011
 * Author		: Ngoc Vo
 * Parameter	: TextView tvDate, TextView tvTime, TextView tvFlowBackDate
 * Input		: TextView that display date and time
 * Output		: None
 * Description	: Update system datetime variables with updated values
 * 				  Update system datetime variables with updated values
 * 				  Set default value for TextView tvFlowBackDate
 * 				  Update strDate, sttrTime with update db format values
 * Modify date	:
 * Modify log	:
 */	
	public static void updateCurrentDate(TextView tvDate, TextView tvTime, TextView tvFlowBackDate){
		
		final Calendar c = Calendar.getInstance();
		intYear = c.get(Calendar.YEAR);
		intMonth = c.get(Calendar.MONTH) + 1;
		intDay = c.get(Calendar.DAY_OF_MONTH);
		intHour = c.get(Calendar.HOUR_OF_DAY);
		intMinute = c.get(Calendar.MINUTE);
		intSecond = c.get(Calendar.SECOND);
		
		tvFlowBackDate.setText(R.string.txt_NgayTraDisplay);
		
		updateTime(tvTime);
		updateDate(tvDate);
	}
/*
 * ID			: 08
 * Method name	: SaveCashFlow
 * Create date	: 08-03-2011
 * Author		: Ngoc Vo
 * Parameter	: Context context, FlowType flowType, final EditText edit_Amount , 
 * 				  String Amount, String Name, String FlowBackDate
 * Input		: The information that user entered on UI
 * Output		: None
 * Description	: Put the information that user entered on UI
 * 				  into the CashFlow object then save the object into database
 * Modify date	:
 * Modify log	:
 */	
	public static void SaveCashFlow(Context context, FlowType flowType, final EditText edit_Amount , String Amount, String Name, String FlowBackDate) throws ParseException{
		/*
		 * if 
		 * 		The value of amount is empty or equal to 0
		 * then
		 * 		Show "Information Missing" dialog
		 * else
		 * 		Create a CashFlow object
		 * 		Create a CashFlowData object
		 * 		Insert value of the attributes into the CashFlow object
		 * 		Insert CashFlow object in to database.
		 */
		if(Amount.equals("") || Integer.parseInt(Amount) == 0){
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			builder.setTitle(Global.strMissingInfo);
			builder.setMessage(Global.strMissingAmount);
			builder.setPositiveButton(Global.strPositiveButton, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					edit_Amount.setText("");					
				}
			});
			builder.show();
		}else{
								
			//Create new CashFlow object
			CashFlow cashFlow = new CashFlow();
			CashFlowData CFData = new CashFlowData(context);
			
			//Add properties
			cashFlow.setId(System.currentTimeMillis());
			cashFlow.setAmount(Integer.parseInt(Amount));										
			cashFlow.setFlowType(flowType.type());
			cashFlow.setCreateDate(dbFormat.parse( strCreateDate.toString()));
			cashFlow.setModifiedDate(dbFormat.parse(strCreateDate.toString()));
			cashFlow.setFlowDate(dbFormat.parse(strDate + strTime));			
			cashFlow.setStatus(srtStatus);
			
			//Assign value for Name 
			if(Name.equals("")){
				cashFlow.setName(strKhoanThu);
			}else{
				cashFlow.setName(Name);
			}
			
			//Assign value for FlowBackDate
			if(FlowBackDate.equals(R.string.txt_NgayTraDisplay)||FlowBackDate.equals("")){
				Date date = null;
				cashFlow.setFlowBackDate(date);
			}else{
				cashFlow.setFlowBackDate(dbFormat.parse(Global.strFlowBackDate + Global.strTime));
			}
			
			try{	
				//Add an object CashFlow to db
				CFData.Add(cashFlow);
			}catch (Exception e) {
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				builder.setTitle("Error orrcurs!");
				builder.setMessage("The error is: " + e.getMessage());
				builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
													
					}
				});
				builder.show();
			}
		}
	}
	
}
/*
 * Define enum type: FlowType
 * Unknow	: -1
 * Thu		: 0
 * Chi		: 1
 * Vay		: 2
 * No		: 3	
 */
	enum FlowType {
	    Unknown (-1),
	    Thu (0),
	    Chi (1),
	    ChoVay (2),
	    No(3);
	
	    private final int type;
	
	    FlowType(int type) {
	        this.type = type;
	    }
	
	    public int type () { 
	        return type; 
	    }
	}

